package com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xidian.zhiancloud.dao.DangerousChemicalInfoMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.model.DangerCategory;
import com.xidian.zhiancloud.model.DangerousChemicalStorage;
import com.xidian.zhiancloud.model.DangerousChemicalWarehouseOut;
import com.xidian.zhiancloud.model.WasteStorage;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.wastePart.WasteStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 　　* @program zhiancloud
 * 　　* @author Egoist
 * 　　* @create 2021/1/29 10:06
 */
@Service
public class HomeService {

    @Autowired
    DangerousChemicalWarehouseOutService dangerousChemicalWarehouseOutService;
    @Autowired
    WasteStorageService wasteStorageService;
    @Autowired
    DangerCategoryService dangerCategoryService;
    @Autowired
    DangerousChemicalStorageService dangerousChemicalStorageService;
    @Autowired
    OrganizationMapper organizationMapper;
    @Autowired
    DangerousChemicalInfoMapper dangerousChemicalInfoMapper;

    public Map<String, Integer> getStaticInfo(Long companyId, Long userId) {
        Map<String, Integer> map = new HashMap<>();
        int[] num = new int[8];
        int reviewNum = 0;
        for (int i = 0; i < 8; i++) {
            QueryWrapper<DangerousChemicalWarehouseOut> qw = new QueryWrapper();
            qw.eq("state", i).eq("company_id", companyId);
            switch (i) {
                case 1:
                    List<DangerousChemicalWarehouseOut> list = dangerousChemicalWarehouseOutService.list(qw);
                    for (DangerousChemicalWarehouseOut out : list) {
                        String[] split = out.getReviewerIds().split(",");
                        List<String> listIds = Arrays.asList(split);
                        if (listIds.contains(userId.toString())){
                            reviewNum++;
                        }
                    }
                    break;
                case 2:
                    qw.eq("outer_id", userId);
                    break;
                case 3:
                case 4:
                    qw.eq("applicant_id", userId);
                    break;
            }
            num[i] = dangerousChemicalWarehouseOutService.count(qw);
        }
        map.put("待审批", reviewNum);
        map.put("待出库", num[2]);
        map.put("待领取", num[3]);
        map.put("待归还", num[4]);
        QueryWrapper<WasteStorage> qw2 = new QueryWrapper<>();
        qw2.eq("state", 0).eq("company_id", companyId);
        map.put("待报废", wasteStorageService.count(qw2));

        return map;
    }

    // 数据结构为 [1类易燃液体, 2类易燃液体, 3类易燃液体, ...]
    public List<String> getTypeName(Long companyId) {
        List<String> typeName = new ArrayList<>();
        QueryWrapper<DangerCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        List<DangerCategory> list = dangerCategoryService.list(queryWrapper);
        for (DangerCategory dangerCategory : list) {
            typeName.add(dangerCategory.getName());
        }
        return typeName;
    }

    // 数据结构为 [{部门:环保部, 1类易燃液体:false, 2类易燃液体:true, ....}, {部门:生产部, 1类易燃液体:false, 2类易燃液体:true, ....},.....]
    public List<Map> getTypeInDepartment(Long companyId) {
        List<Map> dataSource = new ArrayList<>();
        List<String> dangerCategoryName = new ArrayList<>();
        Set<Long> organizationIds = new HashSet<>();
        QueryWrapper<DangerousChemicalStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        List<DangerousChemicalStorage> storageList = dangerousChemicalStorageService.list(queryWrapper);

        //查出危化品入库记录中的所有部门id，并加入Set中防止部门重复
        for (DangerousChemicalStorage dangerousChemicalStorage : storageList) {
            Long organizationId = dangerousChemicalStorage.getOrganizationId();
            organizationIds.add(organizationId);
        }

        //查出危化品的所有类型名
        List<DangerCategory> dangerCategoryList = dangerCategoryService.list();
        for (DangerCategory dangerCategory : dangerCategoryList) {
            dangerCategoryName.add(dangerCategory.getName());
        }

        // Map中第一个键值对为部门名，其余为该部门是有拥有某种类型危化品的键值对
        int count = 0;  //Map数组的索引
        for (Long id : organizationIds) {
            Map data = new HashMap();

            Organization organization = organizationMapper.selectById(id);
            data.put("部门", organization == null ? "该部门已删除" : organization.getName());
            //找出该部门的所有危化品库存记录
            QueryWrapper<DangerousChemicalStorage> qw = new QueryWrapper<>();
            qw.eq("organization_id", id).eq("company_id", companyId);
            List<DangerousChemicalStorage> dangerousChemicalStorageList = dangerousChemicalStorageService.list(qw);
            //找出该部门拥有的所有危化品类型名称
            List<String> haveCategoryList = new ArrayList<>();
            for (DangerousChemicalStorage dangerousChemicalStorage : dangerousChemicalStorageList) {
                Long chemicalId = dangerousChemicalStorage.getChemicalId();
                DangerCategory dangerCategory = dangerCategoryService.getById(chemicalId);
                haveCategoryList.add(dangerCategory == null ? "该类型已删除" : dangerCategory.getName());
            }
            //如果该部门拥有某种类型的危化品就标记为true,否则为false
            for (String s : dangerCategoryName) {
                if (haveCategoryList.contains(s)) {
                    data.put(s, true);
                } else {
                    data.put(s, false);
                }
            }
            dataSource.add(data);
        }
        return dataSource;
    }


}
